/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
#ifndef PLLINKEDLIST_H
#define PLLINKEDLIST_H

#include <tools/PLRingList.h>

namespace PLSDK {
    
    /**
     * Linked list.
     */
    template<typename T>
    class PLLinkedList : public PLRingList<T> {
    public:
        /**
         * Constructor.
         */
        PLLinkedList() : PLRingList<T>() {}
        /**
         * Copy constructor.
         *
         * @param list The list to copy.
         */
        PLLinkedList( const PLLinkedList<T> &list );
        /**
         * Add a new node to the list with the object object.
         *
         * @param object The object to add to the list.
         * @param current The current object after that we add the item.
         */
        inline void add(T object, PLLinkedListNode<T> *current = 0);
        /**
         * Append a new object.
         *
         * @param object Object to append.
         */
        inline virtual void append(T object) {
            add(object);
        }
        /**
         * Prepend a new object.
         *
         * @param object Object to prepend.
         */
        inline virtual void prepend(T object) {
            add(object, static_cast<PLLinkedListNode<T> *>(PLList<T>::m_first));
        }
        /**
         * Get the last node of the list.
         *
         * @return The last node.
         */
        inline virtual PLListNode<T> &lastNode() {
            return *m_last;
        }
    private:
        PLLinkedListNode<T> *m_last;
        PLLinkedList<T>& operator=( const PLLinkedList<T> &copy );
    };

    template<typename T>
    PLLinkedList<T>::PLLinkedList( const PLLinkedList<T> &list ) {
        PLLinkedListIterator<T> it = list.iterator();
        while(it.hasNext()) {
            add(it.next());
        }
    }

    template<typename T>
    void PLLinkedList<T>::add(T object, PLLinkedListNode<T> *current) {
        PLLinkedListNode<T> *tempNode = new PLLinkedListNode<T>(object);
        if(current != 0) {
            tempNode->setNextNode(current->next());
            tempNode->setPreviousNode(current);
            current->setNextNode(tempNode);
        } else {
            if(PLList<T>::m_first->next() != 0) {
                m_last->setNextNode(tempNode);
                tempNode->setPreviousNode(m_last);
            } else {
                PLList<T>::m_first->setNextNode(tempNode);
                tempNode->setPreviousNode(static_cast<PLLinkedListNode<T> *>(PLList<T>::m_first));
            }
            m_last = tempNode;
        }
    }

}

#endif
